package edu.imac.angryfood.component;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.concurrent.CopyOnWriteArrayList;

import javax.swing.Timer;

import org.jbox2d.common.Vec2;
import org.newdawn.slick.SlickException;

import edu.imac.angryfood.world.World;

/** A projectile that explodes when the user clicks and it is still in the air */

public class BombProjectile extends AbstractProjectile {
	/** contains the fragments of the bomb when it has exploded */
	private CopyOnWriteArrayList<AbstractProjectile> projArray;
	/** the coordinate where the explosion has happend */
	private Vec2 explosionCoord;
	/** timer to delete the remaining fragments of the bomb */
	private Timer t;

	
	/** constructor for circle projectiles
	 * 
	 * @param x x position of the projectile
	 * @param y y position of the projectile
	 * @param radius radius of the projectile
	 * @param droped if the projectile has already been droped
	 * @param num the order in which the projectile will be launched
	 * @param image the name of the image's file
	 */
	
	public BombProjectile(int x, int y, float radius, boolean droped, int num, String image)
			throws SlickException {
		super(x, y, radius, droped, num, image);
		projArray = new CopyOnWriteArrayList<AbstractProjectile>();
		collided=false;
		
	}
	
	/** constructor for rectangle projectiles
	 * 
	 * @param x x position of the projectile
	 * @param y y position of the projectile
	 * @param width width of the projectile
	 * @param height height of the projectile
	 * @param droped if the projectile has already been droped
	 * @param num the order in which the projectile will be launched
	 * @param image the name of the image's file
	 */
	
	public BombProjectile(int x, int y, int width, int height, boolean droped, int num, String image)
			throws SlickException {
		super(x, y, width, height, droped, num, image);
		projArray = new CopyOnWriteArrayList<AbstractProjectile>();
		collided=false;
	}

	public Vec2 getExplosionCoord() {
		return explosionCoord;
	}

	public CopyOnWriteArrayList<AbstractProjectile> getProjArray() {
		return projArray;
	}

	/** the bomb explodes when the user clicks
	 * 
	 * @param world the world that contains the game
	 * @throws SlickException
	 */
	
	@Override
	public void click(final World world) throws SlickException {
		if (tLaunched instanceof Timer){
			tLaunched.stop();
		}
		next(world);
		Vec2 boom = new Vec2();
		Vec2 center = new Vec2();
		double teta;
		//creation of 20 fragments at the explosion
		for (int i = 0; i < 20; i++) {
			teta = 2 * i * 3.14 / 20;
			boom.x = (float) Math.cos(teta) * 100000;
			boom.y = (float) Math.sin(teta) * 100000;
			center.x = (float) (getJboxBody().getWorldCenter().x + (getRadius())
					* Math.cos(teta));
			center.y = (float) (getJboxBody().getWorldCenter().y + (getRadius())
					* Math.sin(teta));
			explosionCoord = getJboxBody().getWorldCenter();

			AbstractProjectile proj = new SimpleProjectile((int) center.x, (int) center.y,10,true, -1, "rond.png");
			proj.setShow(false);
			world.add(proj);
			proj.getJboxBody().applyImpulse(boom, center);
			projArray.add(proj);
		}
		//the bomb is hidden
		setShow(false);
		
		//After 1 second, the bomb and all the fragments that are still living are deleted
		t = new Timer (1000, new ActionListener(){
			public void actionPerformed(ActionEvent e){
				delete(world);
				for (AbstractProjectile proj:projArray){
					world.remove(proj);
					projArray.remove(proj);
				}				
				t.stop();
			}
		});
		t.start();
	}
	
	/** All the fragments too far from the explosion coordinates are deleted (to limit the size of the explosion's area )
	 * 
	 * @param world the world that contains the game
	 */
	
	public void checkFragments(World world){

		for (AbstractProjectile proj:projArray){
			Vec2 coord=proj.getJboxBody().getWorldCenter();
			Vec2 diff=new Vec2();
			diff.x=explosionCoord.x-coord.x;
			diff.y=explosionCoord.y-coord.y;
			if (diff.length()>100){
				world.remove(proj);
				projArray.remove(proj);
			}
		}
	}
	
	/** delete the bomb from the world
	 * 
	 * @param world the world that contains the game
	 */
	public void delete(World world){
		world.remove(this);
	}

	/** when the bomb collides something, it explodes
	 * 
	 * @param world the world that contains the game
	 */
	@Override
	public void collision(final World world) {
		
		if (!collided){
			if (tLaunched instanceof Timer){
				tLaunched.stop();
			}
			collided=true;

			try {
				click(world);
			} catch (SlickException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

			
		}
	}

}